/*
* "PS2" Application Framework
*
* University of Abertay Dundee
* May be used for educational purposed only
*
* Author - Dr Henry S Fortuna
*
* $Revision: 1.5 $
* $Date: 2007/08/27 20:22:34 $
*
*/

#include <sps2lib.h>
#include <sps2tags.h>
#include <sps2util.h>
#include <sps2regstructs.h>
#include <assert.h>
#include <memory.h>
#include <signal.h>
#include "PS2Defines.h"
#include "ps2maths.h"
#include "ps2matrix4x4.h"
#include "pad.h"
#include "sps2wrap.h"
#include "dma.h"
#include "texture.h"
#include "font.h"
#include "pipeline.h"
#include "entities.h"
#include "vumanager.h"
#include "timer.h"
#include "ms3dmodel.h"
#include "terrain.h"

#include "ps2game.h"

/*
	Look at the start of the vcl code to get the positions
	and layout of the packet data in VU data memrory.
	Remember that the data sent to the VU must be in
	the specified format of the PS2 WILL CRASH

*/

bool g_bLoop = true;

void sig_handle(int sig) {
    g_bLoop = false;
}


int main(void) {
    Game* game = new PS2Game();
    game->Run();

    // Performance timer - call after SPS2Manager.Initialise()
    CTimer Timer;

    // Initialise control pad 0
    if (!pad_init(PAD_0, PAD_INIT_LOCK | PAD_INIT_ANALOGUE | PAD_INIT_PRESSURE)) {
        printf("Failed to initialise control pad\n");
        pad_cleanup(PAD_0);
        exit(0);
    }

    // Register our signal function for every possible signal (i.e. ctrl + c)
    for (int sig=0; sig<128; sig++)
        signal(sig, sig_handle);

    float timeDelta = 0;

    // The main Render loop
    while (g_bLoop && game->IsRunning()) {
        //start the time
        //Timer.PrimeTimer();

        VIFDynamicDMA.Fire();
        SPS2Manager.BeginScene();
        game->Update(0.016666f);
        game->Draw(0.016666f);
        SPS2Manager.EndScene();

        timeDelta = Timer.GetTimeDelta();
        game->SetFPS(Timer.GetFPS(timeDelta));
        timeDelta *= 0.001f;
        if (pad[0].pressed & PAD_TRI)SPS2Manager.ScreenShot();
    }

    pad_cleanup(PAD_0);

    return 0;
}

